package com.module_live.fragment.coachstudy.workrecord;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.px_dp.DisplayUtil;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.circle_image.niv.NiceImageView;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.ui.fragment.BaseAppFragment;
import com.lib.utls.glide.GlideConfig;
import com.module_live.R;
import com.module_live.R2;
import com.module_live.activity.CoachStudyLivingActivity;
import com.module_live.bean.UploadVoice;
import com.module_live.bean.coachstudy.TeacherDealedVoiceWorkBean;
import com.module_live.view.CommonSoundItemView;
import com.module_live.view.ZoomImagePopupUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class RecordTeacherDealedFragment extends BaseAppFragment {

    @BindView(R2.id.el_work)
    ExpandableListView elWork;

    public String course_id;

    TeacherDealedWorkAdapter dealedWorkAdapter;

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        getStudentFinishedWorkList();

        dealedWorkAdapter = new TeacherDealedWorkAdapter();
        elWork.setAdapter(dealedWorkAdapter);

        elWork.setGroupIndicator(null);
    }

    List<TeacherDealedVoiceWorkBean.ObjBean> finishedList = new ArrayList<>();

    /**
     * 获取学生已提交作业列表
     */
    public void getStudentFinishedWorkList() {

        HttpUtils.with(getActivity())
                .addParam("requestType", "STUDENT_WORK_RECORD")
                .addParam("token", SharedPreferenceManager.getInstance(getActivity()).getUserCache().getUserToken())
                .addParam("course_id", CoachStudyLivingActivity.course_id)
                .addParam("edit_or_view", 0)
                .addParam("recording_manner", 2)
                .execute(new HttpNormalCallBack<TeacherDealedVoiceWorkBean>() {
                    @Override
                    public void onSuccess(TeacherDealedVoiceWorkBean result) {
                        if (CodeUtil.CODE_200 == result.getCode()) {

                            if (finishedList.size() > 0) {
                                finishedList.clear();
                            }

                            finishedList.addAll(result.getObj());
                            dealedWorkAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_study_record_detail;
    }


    class TeacherDealedWorkAdapter extends BaseExpandableListAdapter {

        @Override
        public int getGroupCount() {
            return finishedList.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return finishedList.get(groupPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupViewHolder groupViewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_study_setwork_top, parent, false);
                groupViewHolder = new GroupViewHolder(convertView);
                convertView.setTag(groupViewHolder);
            } else {
                groupViewHolder = (GroupViewHolder) convertView.getTag();
            }
            //设置用户头像及用户姓名
            if (StringUtil.isNotEmpty(finishedList.get(groupPosition).getPhoto_url(), true)) {

                Glide.with(getActivity())
                        .load(finishedList.get(groupPosition).getPhoto_url())
                        .apply(GlideConfig.getCircleOptions())
                        .into(groupViewHolder.niceImageView);
            }

            if (StringUtil.isNotEmpty(finishedList.get(groupPosition).getUser_name(), true)) {
                groupViewHolder.tvName.setText(finishedList.get(groupPosition).getUser_name());
            }

            if (isExpanded) {
                groupViewHolder.ivArrow.setBackgroundResource(R.mipmap.icon_up);
            } else {
                groupViewHolder.ivArrow.setBackgroundResource(R.mipmap.icon_down);
            }

            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return finishedList.get(groupPosition).getCorrect_homework_image().get(childPosition);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            final ChildViewHolder childViewHolder;
            if (convertView == null) {

                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_study_record_deal_rv, parent, false);
                childViewHolder = new ChildViewHolder(convertView);
                convertView.setTag(childViewHolder);
            } else {
                childViewHolder = (ChildViewHolder) convertView.getTag();
            }

            if (finishedList.get(groupPosition).getCorrect_homework_image() != null) {
                if (finishedList.get(groupPosition).getCorrect_homework_image().size() > 0) {
                    //为recycleview设置适配器和数据---gridviewmanager
                    FinishedWorkImgAdapter imgAdapter = new FinishedWorkImgAdapter(getActivity(), finishedList.get(groupPosition).getCorrect_homework_image());
                    MyLinearLayoutManager manager = new MyLinearLayoutManager(getActivity(), MyLinearLayoutManager.HORIZONTAL, false);
                    childViewHolder.rvFinishedImg.setLayoutManager(manager);
                    childViewHolder.rvFinishedImg.setAdapter(imgAdapter);
                }
            }

            if (finishedList.get(groupPosition).getVoice() != null) {
                if (finishedList.get(groupPosition).getVoice().size() > 0) {
                    FinishedWorkVoiceAdapter voiceAdapter = new FinishedWorkVoiceAdapter(getActivity(), finishedList.get(groupPosition).getVoice());
                    MyLinearLayoutManager manager1 = new MyLinearLayoutManager(getActivity(), MyLinearLayoutManager.HORIZONTAL, false);
                    childViewHolder.rvFinishedVoice.setLayoutManager(manager1);
                    childViewHolder.rvFinishedVoice.setAdapter(voiceAdapter);
                }
            }

//            if (finishedList.get(groupPosition).getVoice() != null) {
//                if (finishedList.get(groupPosition).getVoice().size() > 0) {
//                    TeacherVoiceListAdapter voiceAdapter = new FinishedWorkVoiceAdapter(getActivity(), finishedList.get(groupPosition).getVoice());
//                    MyLinearLayoutManager manager1 = new MyLinearLayoutManager(getActivity(), MyLinearLayoutManager.HORIZONTAL, false);
//                    childViewHolder.rvFinishedVoice.setLayoutManager(manager1);
//                    childViewHolder.rvFinishedVoice.setAdapter(voiceAdapter);
//                }
//            }

            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }


    static class GroupViewHolder {
        public NiceImageView niceImageView;
        public TextView tvName;
        public ImageView ivArrow;

        public GroupViewHolder(View view) {

            niceImageView = view.findViewById(R.id.iv_user);
            tvName = view.findViewById(R.id.tv_name);
            ivArrow = view.findViewById(R.id.iv_arrow);
        }

    }

    static class ChildViewHolder {
        RecyclerView rvFinishedImg;

        RecyclerView rvFinishedVoice;

        public ChildViewHolder(View view) {

            rvFinishedImg = view.findViewById(R.id.rv_img);
            rvFinishedVoice = view.findViewById(R.id.rv_voice);
        }
    }

    // TODO: 2020/9/29 0029   作业图片适配器
    class FinishedWorkImgAdapter extends BaseAdapter<String> {

        public FinishedWorkImgAdapter(Context context, List<String> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_study_finished_rv_child;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<String> mData) {

            ImageView ivWork = holder.getView(R.id.iv_work);
            if (StringUtil.isNotEmpty(mData.get(position), true)) {

                Glide.with(getActivity())
                        .load(mData.get(position))
                        .apply(GlideConfig.getRoundOptions(10))
                        .into(ivWork);
            }

            ivWork.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ZoomImagePopupUtils imagePopupUtils = new ZoomImagePopupUtils(getActivity());
                    imagePopupUtils.setZoomImage("otheer", mData.get(position));
                    imagePopupUtils.showAnswerPopuPopu(v);

                }
            });

        }
    }


    // TODO: 2020/9/29 0029   作业科目和语音适配器
    class FinishedWorkVoiceAdapter extends BaseAdapter<TeacherDealedVoiceWorkBean.ObjBean.VoiceBean> {


        public FinishedWorkVoiceAdapter(Context context, List<TeacherDealedVoiceWorkBean.ObjBean.VoiceBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_study_record_deal_rv_voice;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<TeacherDealedVoiceWorkBean.ObjBean.VoiceBean> mData) {

            if (StringUtil.isNotEmpty(mData.get(position).getSubject(), true)) {

                holder.setText(R.id.tv_subj, mData.get(position).getSubject());
            }

            RecyclerView rvVoice = holder.getView(R.id.rv_voice);

            if (mData.get(position).getVoice_description() != null) {
                if (mData.get(position).getVoice_description().size() > 0) {
                    TeacherVoiceListAdapter voiceAdapter = new TeacherVoiceListAdapter(getActivity(), mData.get(position).getVoice_description());
                    MyLinearLayoutManager manager = new MyLinearLayoutManager(getActivity());
                    rvVoice.setLayoutManager(manager);
                    rvVoice.setAdapter(voiceAdapter);
                }
            }
        }

    }

    //语音列表
    class TeacherVoiceListAdapter extends BaseAdapter<TeacherDealedVoiceWorkBean.ObjBean.VoiceBean.VoiceDescriptionBean> {

        public TeacherVoiceListAdapter(Context context, List<TeacherDealedVoiceWorkBean.ObjBean.VoiceBean.VoiceDescriptionBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_study_finished_voice;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<TeacherDealedVoiceWorkBean.ObjBean.VoiceBean.VoiceDescriptionBean> mData) {

            LinearLayout linVoice = holder.getView(R.id.lin_item_voice);
            String voicePlayUrl = mData.get(position).getVoice_hash();

            if (StringUtil.isNotEmpty(voicePlayUrl, true)) {

                instertVoice(linVoice, voicePlayUrl, true);
            }

        }

    }

    private void instertVoice(LinearLayout linVocie, String url, boolean isDletedAble) {

        UploadVoice uploadVoice = new UploadVoice();
        uploadVoice.setPlayUrl(url);

        final CommonSoundItemView commonSoundItemView = new CommonSoundItemView(getActivity());
        commonSoundItemView.isLocalVoice(false);

        commonSoundItemView.isDletedAble(isDletedAble);

        commonSoundItemView.setAudioEntity(uploadVoice);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        params.topMargin = DisplayUtil.dip2px(getActivity(), 16);
        commonSoundItemView.setLayoutParams(params);

        linVocie.addView(commonSoundItemView);

    }
}
